/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatserver;


import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;


public class HttpHandshake implements Runnable {


    private int port;
    private ServerSocket ss;
    final static String HTTP_REPLY[] = {"HTTP/1.1 200 OK\r\n"
        + "Server: super-puper krootoi serv\r\n"
        + "X-Powered-By: tomatoes\r\n"
        + "Content-Type: text/html; charset=utf-8\r\n"
        + "Content-Length: ",
        " \r\nConnection: close\r\n\r\n"};
    final static String HTML[] = {"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html\"; charset=utf-8\" /></head><body>",
        "</body><html>"};
    final static String HANDSHAKE[] = {"<handshake version=4 token=", "/>"};
    private byte buf[] = new byte[64 * 1024];


    HttpHandshake(int port) {
        this.port = port;
    }


    @Override
    public void run() {
        waitForConnection();
    }


//TODO HANDSHAKER
    private void waitForConnection() {
        try {
            ss = new ServerSocket(port, 0);
            System.out.println("Socket in HttpHandshake created");
            while (true) {
                Handshaker hh = new Handshaker(ss.accept());
                Thread t = new Thread(hh);
                t.setDaemon(true);
                t.start();
            }
        } catch (IOException ex) {
            Logger.getLogger(HttpHandshake.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    public class Handshaker implements Runnable {


        private Socket s;


        Handshaker(Socket s) {
            this.s = s;
        }


        @Override
        public void run() {
            send();
        }


        private void send() {
            try {
                String received = new String(buf, 0, s.getInputStream().read(buf));
             //   System.out.println("Connected to handshaker \n" + received + "\n");
                String token = ChatServer.getHash(s.getInetAddress().getAddress());


                String handshake = HANDSHAKE[0].concat(token).concat(HANDSHAKE[1]);
                String html = HTML[0].concat(handshake).concat(HTML[1]);
                String hLength=new Integer(html.length()).toString();
             
                String httpReply = HTTP_REPLY[0].concat(hLength).concat(HTTP_REPLY[1]);
                String toSend = httpReply.concat(html);
               // System.out.println(toSend);
                s.getOutputStream().write(toSend.getBytes("utf-8"));
            } catch (Exception ex) {
                Logger.getLogger(HttpHandshake.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                closeSocket();
            }
        }


        private void closeSocket() {
            try {
                s.close();
            } catch (IOException ex) {
                Logger.getLogger(HttpHandshake.class.getName()).log(Level.SEVERE, null, ex);
            }


        }
    }
}